﻿using JetBrains.Annotations;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Localization;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Threading;

namespace YaAppoint;

public static class YaAppointModuleExtensionConfigurator
{
    private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();

    public static void Configure()
    {
        OneTimeRunner.Run(() =>
        {
            ConfigureExistingProperties();
            ConfigureExtraProperties();
        });
    }

    private static void ConfigureExistingProperties()
    {
        /* You can change max lengths for properties of the
         * entities defined in the modules used by your application.
         *
         * Example: Change user and role name max lengths

           AbpUserConsts.MaxNameLength = 99;
           IdentityRoleConsts.MaxNameLength = 99;

         * Notice: It is not suggested to change property lengths
         * unless you really need it. Go with the standard values wherever possible.
         *
         * If you are using EF Core, you will need to run the add-migration command after your changes.
         */
    }

    // 扩展依赖模块的实体，配置额外属性
    private static void ConfigureExtraProperties()
    {
        OneTimeRunner.Run(() => {
            // 扩展 IdentityUser
            ObjectExtensionManager.Instance.Modules()
                      .ConfigureIdentity(identity => // 配置Identity模块
                      {
                          identity.ConfigureUser(user => { // 配置IdentityUser实体
                              user.AddOrUpdateProperty<string>(
                                  "JobNumber",
                                  property =>
                                  {
                                      property.Attributes.Add(new CanBeNullAttribute());
                                      property.Attributes.Add(new MaxLengthAttribute(64));
                                      property.DisplayName = new FixedLocalizableString("工号");
                                  }
                              );
                              user.AddOrUpdateProperty<string>(
                                "IdNumber",
                                property =>
                                {
                                    property.Attributes.Add(new CanBeNullAttribute());
                                    property.Attributes.Add(new MaxLengthAttribute(50));
                                    property.DisplayName = new FixedLocalizableString("身份证号");
                                }
                              );


                              user.AddOrUpdateProperty<string>(
                                  "OuterNumber",
                                  property =>
                                  {
                                      property.Attributes.Add(new MaxLengthAttribute(50));
                                      property.DisplayName = new FixedLocalizableString("外部编号");
                                  }
                              );

                              user.AddOrUpdateProperty<string>(
                                "Description",
                                property => {
                                    property.Attributes.Add(new CanBeNullAttribute());
                                    property.Attributes.Add(new DataTypeAttribute(DataType.Text));
                                    property.Attributes.Add(new MaxLengthAttribute(200));
                                    property.UI.OnTable.IsVisible = false;
                                    property.UI.Order = 1000;
                                    property.DisplayName = new FixedLocalizableString("描述");
                                }
                              );
                          });
                      });

          
        });
    }
}
